/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-06-01     tyustli     the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_touch.h"
#include "drv_touch_gt911.h"

#if TOUCH_USE_GT911

#include <string.h>
#include <stdlib.h>

#define TOUCH_I2C I2C1

#define GT9xx_TS_ADDR_HIGH	(0x5D <<1)
#define GT9xx_TS_ADDR_LOW		(0x14 <<1)

//#define CFG_VERSION 0x4780
//#define PRODUCT_ID  0x4081
//#define State_ADD   0x4E81
//#define COOR_ADD    0x4E81
//#define CFG911_LEN  186

//I2C��д����	
#define GT_CMD_WR 		0X28     	//д����
#define GT_CMD_RD 		0X29		//������
  
/********************************GT911���ּĴ�������***************************/
#define GT_CTRL_REG 	0X8040   	//GT911���ƼĴ���
#define GT_CFGS_REG 	0X8047   	//���ð汾
#define GT_X_MAX_LOW 	0X8048   	//X����ֽ�
#define GT_X_MAX_HOW 	0X8049   	//X����ֽ�
#define GT_Y_MAX_LOW 	0X804A   	//Y����ֽ�
#define GT_Y_MAX_HOW 	0X804B   	//Y����ֽ�
#define GT_TOUCH_NUM  0X804C      //�����������1--10


#define GT_CHECK_REG 	0X80FF   	//GT911У��ͼĴ���
#define GT_PID_REG 		0X8140   	//GT911��ƷID�Ĵ���

#define GT_GSTID_REG 	0X814E   	//��ǰ��⵽�Ĵ������

#define GT_TP1_REG 		0X814F  	//��һ�����������ݵ�ַ
#define GT_TP2_REG 		0X8157		//�ڶ������������ݵ�ַ
#define GT_TP3_REG 		0X815F		//���������������ݵ�ַ
#define GT_TP4_REG 		0X8167		//���ĸ����������ݵ�ַ
#define GT_TP5_REG 		0X816F		//��������������ݵ�ַ

//#define GT_TP1_REG 		0X8150  	//��һ�����������ݵ�ַ
//#define GT_TP2_REG 		0X8158		//�ڶ������������ݵ�ַ
//#define GT_TP3_REG 		0X8160		//���������������ݵ�ַ
//#define GT_TP4_REG 		0X8168		//���ĸ����������ݵ�ַ
//#define GT_TP5_REG 		0X8170		//��������������ݵ�ַ

//GT911���ò�����
//��һ���ֽ�Ϊ�汾��(0X60),���뱣֤�µİ汾�Ŵ��ڵ���GT911�ڲ�
//flashԭ�а汾��,�Ż��������.
//uint8_t GT911_CFG_TBL[]=
//{ 
//	0x00,0x10,0x01,0xE0,0x01,0x05,0x0D,0x00,0x01,0x08,0x28,0x05,0x50,0x32,0x03,0x05,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x87,0x28,0x0A,0x17,0x15,0x31,0x0D,0x00,0x00,0x01,0xB9,0x04,0x25,
//	0x00,0x00,0x00,0x00,0x00,0x03,0x64,0x32,0x00,0x00,0x00,0x0F,0x94,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,
//	0x8D,0x13,0x00,0x5D,0x1E,0x00,0x3E,0x30,0x00,0x2C,0x4C,0x00,0x23,0x78,0x00,0x23,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,
//	0x04,0x02,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x24,0x26,0xFF,0xFF,0xFF,
//	0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x45,0x01
//};

//uint8_t GT911_CFG_TBL[]=
//{
//	0x41,0xE0,0x01,0x10,0x01,0x0A,0x0D,0x00,0x01,0x08,0x28,0x05,0x50,0x32,0x03,0x05,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x89,0x2A,0x0B,0x17,0x15,
//	0x31,0x0D,0x00,0x00,0x03,0xBA,0x04,0x2D,0x00,0x00,0x00,0x00,0x00,0x03,0x64,0x32,
//	0x00,0x00,0x00,0x0F,0x94,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,0x8D,0x13,0x00,0x5C,
//	0x1E,0x00,0x3C,0x30,0x00,0x29,0x4C,0x00,0x1F,0x78,0x00,0x1F,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x16,0x14,0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,0x04,0x02,0xFF,0xFF,0xFF,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,
//	0x04,0x06,0x08,0x0A,0x0F,0x10,0x12,0x16,0x18,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,
//	0x24,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x99,0x01
//};

uint8_t GT911_CFG_TBL[]=
{
	0x00,0xE0,0x01,0x10,0x01,0x0A,0x0D,0x00,0x01,0x08,0x28,0x05,0x50,0x32,0x03,0x05,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x89,0x2A,0x0B,0x17,0x15,
	0x31,0x0D,0x00,0x00,0x03,0xBA,0x04,0x2D,0x00,0x00,0x00,0x00,0x00,0x03,0x64,0x32,
	0x00,0x00,0x00,0x0F,0x94,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,0x8D,0x13,0x00,0x5C,
	0x1E,0x00,0x3C,0x30,0x00,0x29,0x4C,0x00,0x1F,0x78,0x00,0x1F,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x16,0x14,0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,0x04,0x02,0xFF,0xFF,0xFF,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,
	0x04,0x06,0x08,0x0A,0x0F,0x10,0x12,0x16,0x18,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,
	0x24,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x99,0x01
};

uint16_t GT911_TPX_TBL[5]={GT_TP1_REG,GT_TP2_REG,GT_TP3_REG,GT_TP4_REG,GT_TP5_REG};


#include "TK499_I2C.h"

//��GT911д��һ������
//reg:��ʼ�Ĵ�����ַ
//buf:���ݻ�������
//len:д���ݳ���
//����ֵ:0,�ɹ�;1,ʧ��.
uint8_t GT911_WR_Reg(uint16_t reg,uint8_t *buf,uint8_t len)
{
	uint8_t i;
	uint8_t ret=0;
	
	//I2CTXByte(TOUCH_I2C,0, GT_CMD_WR);
	
	I2CTXByte(TOUCH_I2C,0, reg>>8);
	
	I2CTXByte(TOUCH_I2C,0, reg&0XFF);
	
	for(i=0;i<len;i++)
	{	   
		I2CTXByte(TOUCH_I2C,0, buf[i]);		
	}  
	return ret; 
}
//��GT911����һ������
//reg:��ʼ�Ĵ�����ַ
//buf:���ݻ�������
//len:�����ݳ���			  
void GT911_RD_Reg(uint16_t reg,uint8_t *buf,uint8_t len)
{
	uint8_t i;
	
	//I2CTXByte(TOUCH_I2C,0, GT_CMD_WR);//����д����
	
	I2CTXByte(TOUCH_I2C,0, reg>>8);//���͸�8λ��ַ
	
	I2CTXByte(TOUCH_I2C,0, reg&0XFF);//���͵�8λ��ַ
	
	//I2CTXByte(TOUCH_I2C,0, GT_CMD_RD);//���Ͷ�����
	
	for(i=0;i<len;i++)
	{	   
		buf[i] = I2CRXByte(TOUCH_I2C);		
	}    
}
//����GT911���ò���
//mode:0,���������浽flash
//     1,�������浽flash
uint8_t GT911_Send_Cfg(uint8_t mode)
{
	uint8_t buf[2];
	uint8_t i=0;
	buf[0]=0;
	buf[1]=mode;	//�Ƿ�д�뵽GT911 FLASH?  ���Ƿ���籣��
	for(i=0;i<sizeof(GT911_CFG_TBL);i++)
	{
		buf[0]+=GT911_CFG_TBL[i];//����У���
	}
  buf[0]=(~buf[0])+1;
	GT911_WR_Reg(GT_CFGS_REG,(uint8_t*)GT911_CFG_TBL,sizeof(GT911_CFG_TBL));//���ͼĴ�������
	GT911_WR_Reg(GT_CHECK_REG,buf,2);//д��У���,�����ø��±��
	return 0;
}

int touch_gt911_init(void)
{
#if TOUCH_USE_HWI2C
	
	I2C_InitTypeDef I2C_InitStructure;
	GPIO_InitTypeDef  GPIO_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);  //i2c1 clk enable
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
	
	GPIO_PinAFConfig(GPIOB, GPIO_Pin_0 | GPIO_Pin_2, GPIO_AF_I2C); //PB0��PB2����ΪIIC
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0 | GPIO_Pin_2 ;   
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; // ���ÿ�©���
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
  I2C_InitStructure.I2C_Mode = I2C_Mode_MASTER;//��ģʽ
  I2C_InitStructure.I2C_OwnAddress = GT9xx_TS_ADDR_LOW;
  I2C_InitStructure.I2C_Speed = I2C_Speed_STANDARD;
  I2C_InitStructure.I2C_ClockSpeed = 400000;   //�ٶ�����Ϊ400K
	I2C_Init(I2C1, &I2C_InitStructure);
	
	I2C_Send7bitAddress(I2C1, GT9xx_TS_ADDR_LOW, 0);
	I2C_Cmd(I2C1, ENABLE);
#endif

	uint8_t temp[10];
	
//	temp[0]=0X02;			
//	GT911_WR_Reg(GT_CTRL_REG,temp,1);//����λGT911
//	delay_ms(10);
//	temp[0]=0X00;	 
//	GT911_WR_Reg(GT_CTRL_REG,temp,1);	//������λ
//	delay_ms(10);
	
	GT911_RD_Reg(GT_PID_REG,temp,4);//��ȡ��ƷID
	temp[4]=0;
	rt_kprintf("CTP ID:%s\r\n",temp);	//��ӡID

	if(rt_strcmp((char*)temp,"911")==0)//ID==911
	{
 		GT911_RD_Reg(GT_CFGS_REG,temp,1);//��ȡGT_CFGS_REG�Ĵ���
		//if(temp[0]<0X60)//Ĭ�ϰ汾�Ƚϵ�,��Ҫ����flash����
		{
			rt_kprintf("Default Ver:%d\r\n",temp[0]);
			GT911_Send_Cfg(1);//���²���������
		}
		
 		GT911_RD_Reg(GT_CFGS_REG,temp,8);//��ȡGT_CFGS_REG�Ĵ���
		rt_kprintf("gt911 cfg [0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X]\r\n",
			temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7]);
	}
	return 0;
}


#include "lcd_port.h"
#include "touch_port.h"
uint32_t screen_dir = 0;
uint8_t read_buff[8*5] = {0};

int touch_get_raw(uint32_t *raw_x,uint32_t *raw_y)
{
	int rc = 0;
	uint32_t touchX=0,touchY=0;
	uint8_t mode = 0;
	uint8_t temp = 0;
	
	GT911_RD_Reg(GT_GSTID_REG,&mode,1);//��ȡ�������״̬ 		
	if((mode&0XF)&&((mode&0XF)<6))
	{
		//temp=0XFF<<(mode&0XF);
		//if(mode & 0x1)
		{
			GT911_RD_Reg(GT_TP1_REG,read_buff,8);	//��ȡXY����ֵ
			touchX = (uint16_t)(read_buff[2]<<8) | (uint16_t)read_buff[1];//x����
			touchY = (uint16_t)(read_buff[4]<<8) | (uint16_t)read_buff[3];//y����
			
			touchY = (LCD_HEIGHT - touchY);
			//if((touchX>0)&&(touchX<2048))
			{
				if(screen_dir)
				{
					*raw_y = touchX;
					*raw_x = touchY;
				}else
				{
					*raw_x = touchX;
					*raw_y = touchY;
				}
				rc =  1;
			}
		}	
	}
	if((mode&0X80)&&((mode&0XF)<6))
	{
		temp=0;
		GT911_WR_Reg(GT_GSTID_REG,&temp,1);//���־ 		
	}
	return rc;
}

void touch_get_xy(struct touch_message *msg)
{
	uint32_t raw_x = 0,raw_y = 0;
	
	if(touch_get_raw(&raw_x,&raw_y)>0)
	{
		msg->x = raw_x;
		msg->y = raw_y;
		msg->event = TOUCH_EVENT_DOWN;
	}else
	{
		msg->event = TOUCH_EVENT_NONE;
	}
}

//void touch_get_xy(struct touch_message *msg)
//{
//	msg->x = 0;
//	msg->y = 0;
//	msg->event = TOUCH_EVENT_NONE;
//}

/************************** end of file ********************************/

#endif
